package com.example.huitong.fragments;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.huitong.App;
import com.example.huitong.R;
import com.example.huitong.bean.User;
import com.example.huitong.db.DBImpl;
import com.example.huitong.ui.MainHomeActivity;

public class LoginFragment extends Fragment implements OnClickListener{
	
	private EditText mEtName;
	private EditText mEtPassword;
	private Button mBtnLogin;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_login, container, false);
		initViews(rootView);
		return rootView;
	}

	private void initViews(View rootView) {
		// TODO Auto-generated method stub
		mEtName = (EditText) rootView.findViewById(R.id.login_account_et);
		mEtPassword = (EditText) rootView.findViewById(R.id.login_password_et);
		mBtnLogin = (Button) rootView.findViewById(R.id.login_btn);
		mBtnLogin.setOnClickListener(this);
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String name = App.getApp().tmpUserName;
		if(name != null && !"".equals(name)){
			mEtName.setText(name);
			mEtPassword.requestFocus();
		}
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		App.getApp().tmpUserName = null;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String name = mEtName.getText().toString();
		String pwd = mEtPassword.getText().toString();
		if(name == null || pwd == null || "".equals(name) || "".equals(pwd)){
			App.toastShort(getActivity(), "用户名或密码不能为空!");
		} else {
			App.getApp().showProgressDialog(getActivity(), "");
			new LoginTask().execute(name, pwd);
		}
	}
	
	class LoginTask extends AsyncTask<String, Void, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			if(params.length != 2){
				throw new IllegalArgumentException("注册参数长度异常");
			}
			try {
				Thread.sleep(1 * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String name = params[0];
			String pwd = params[1];
			User user = DBImpl.getInstance().getUser(name, pwd);
			return user != null;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			App.getApp().dismissDialog();
			if(!result){
				App.toastShort(getActivity(), "帐户名或密码错误!");
			}else {
				App.toastLong(getActivity(), "登录成功!");
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						getActivity().finish();
						Intent intent = new Intent();
						intent.setClass(getActivity(), MainHomeActivity.class);
						startActivity(intent);
					}
				}, 100);
			}
		}
	}
}
